﻿using SuperMap.Data;
using SuperMap.Realspace;
using SuperMap.UI;
using System;
using System.Drawing;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Tools.Conditions.JJAnalysts;

namespace Ycjw.PipeLine.Map.Tools.Conditions
{
    /// <summary>
    /// 净距分析
    /// </summary>
    public class JJAnalyst: ConditionPanel
    {
        private Sunny.UI.UISymbolLabel uiSymbolLabel2;
        private Sunny.UI.UILabel uiLabel1;
        private Sunny.UI.UILabel uiLabel2;
        private Sunny.UI.UIButton uiButtonSelect1;
        private Sunny.UI.UIButton uiButtonSelect2;
        private Sunny.UI.UILine uiLine1;
        private Sunny.UI.UILine uiLine2;
        private Sunny.UI.UISymbolLabel uiSymbolLabel1;

        public JJAnalyst()
        {
            InitializeComponent();
            this.ConditionPanelName = "净距分析";
            uiButtonSelect1.Click += UiButtonSelect_Click;
            uiButtonSelect2.Click += UiButtonSelect_Click;
            this.SetSize(this.Width, 200);
        }
        private int m_FlagOfSelectLine = 0;
        private void UiButtonSelect_Click(object sender, EventArgs e)
        {
            Sunny.UI.UIButton btn = sender as Sunny.UI.UIButton;
            if(btn == this.uiButtonSelect1)
            {
                m_FlagOfSelectLine = 1;
                uiButtonSelect1.Selected = true;
                uiButtonSelect2.Selected = false;
            }
            else
            {
                m_FlagOfSelectLine = 2;
                uiButtonSelect2.Selected = true;
                uiButtonSelect1.Selected = false;
            }
            if(this.SceneControl != null)
            {
                this.SceneControl.Action = SuperMap.UI.Action3D.Select;
                this.SceneControl.ObjectSelected -= SceneControl_ObjectSelected;
                this.SceneControl.ObjectSelected += SceneControl_ObjectSelected;
            }
        }

        private void SceneControl_ObjectSelected(object sender, SuperMap.UI.ObjectSelectedEventArgs e)
        {
            if (e.Count <= 0)
            {
                return;
            }
            Selection3D[] selection3d = SceneControl.Scene.FindSelection(true);
            if (selection3d.Length == 0) return;
            var selectFeature = selection3d[0].ToRecordset();
            if (selectFeature.GetGeometry().Type != GeometryType.GeoLine3D)
            {
                Sunny.UI.UIMessageTip.Show("请选择一条管线");
                return;
            }
            if (m_FlagOfSelectLine == 1)
            {
                this.uiLabel1.Text = selection3d[0].Layer.Caption + " 中 SMID = " + selectFeature.GetID();
                this.uiLabel1.Tag = selectFeature.GetGeometry();
            }
            else
            {
                this.uiLabel2.Text = selection3d[0].Layer.Caption + " 中 SMID = " + selectFeature.GetID();
                this.uiLabel2.Tag = selectFeature.GetGeometry();
            }
            if (uiLabel1.Tag == null || uiLabel2.Tag == null) return;
            if(uiLabel1.Tag == uiLabel2.Tag)
            {
                Sunny.UI.UIMessageTip.Show("请重新选择一条管线");
                return;
            }

            var geom1 = this.uiLabel1.Tag as SuperMap.Data.GeoLine3D;
            var geom2 = this.uiLabel2.Tag as SuperMap.Data.GeoLine3D;

            var points = DistBetweenLines.NearestPoints(geom1, geom2);

            double getZ(GeoLine3D line)
            {
                return line[0][0].Z;
            }
            if (points != null && points.Length > 1)
            {
                double z1 = double.IsNaN(points[0].Z) ? getZ(geom1) : points[0].Z;
                double z2 = double.IsNaN(points[1].Z) ? getZ(geom2) : points[1].Z;

                var p1 = new Point3D() { X = points[0].X, Y = points[0].Y, Z = z1 };
                var p2 = new Point3D() { X = points[1].X, Y = points[1].Y, Z = z2 };
                Point3Ds ps = new Point3Ds(new Point3D[] { p1, p2 });
                GeoLine3D gl3 = new GeoLine3D(ps);

                var p1H = new Point3D() { X = points[0].X, Y = points[0].Y, Z = 0 };
                var p2H = new Point3D() { X = points[1].X, Y = points[1].Y, Z = 0 };
                Point3Ds psH = new Point3Ds(new Point3D[] { p1H, p2H });
                GeoLine3D gl3H = new GeoLine3D(psH);

                gl3.Style3D = new GeoStyle3D() { 
                    AltitudeMode  = AltitudeMode.Absolute,
                    LineColor = Color.Yellow,
                    LineWidth = 4
                };
                Scene.TrackingLayer.Clear();
                Scene.TrackingLayer.Add(gl3, string.Empty);
                
                var hLength = Geometrist.ComputeLength(gl3H, new PrjCoordSys(4326));
                var vLength = Math.Abs(p1.Z - p2.Z);

                var text = "水平距离：" + hLength.ToString("F2") + "(米)\r\n";
                text += "垂直距离 ： " + vLength.ToString("F2") + "(米)";
                var textLocation = gl3[0][gl3[0].Count - 1];
                GeoText3D geoText = new GeoText3D(new TextPart3D(text.ToString(), textLocation));
                Basic.SuperMap.Styles.SetResultTextStyle(geoText);
                Scene.TrackingLayer.Add(geoText, "MeasureDistance");
            }
            #region 取消
            /*
            var minDis = SuperMap.Data.Geometrist.Distance(geom1, geom2);
            DistBetweenLines.Line minDisLine = null;
            for (var i = 0; i < geom1.PartCount; i++)
            {
                var partPointsInLine1 = geom1[i];
                for (var j = 0; j < partPointsInLine1.Count - 1; j++)
                {
                    var seg1P1 = new DistBetweenLines.Point3D() { X = partPointsInLine1[j].X, Y = partPointsInLine1[j].Y, Z = partPointsInLine1[j].Z };
                    var seg1P2 = new DistBetweenLines.Point3D() { X = partPointsInLine1[j + 1].X, Y = partPointsInLine1[j + 1].Y, Z = partPointsInLine1[j + 1].Z };
                    DistBetweenLines.Line line1 = new DistBetweenLines.Line() { Start = seg1P1, End = seg1P2 };
                    for (var m = 0; m < geom2.PartCount; m++)
                    {
                        var partPointsInLine2 = geom1[i];
                        if (Geometrist.Distance(new GeoLine3D(partPointsInLine1), new GeoLine3D(partPointsInLine2)) > minDis)
                        {
                            continue;
                        }
                        for (var n = 0; n < partPointsInLine2.Count - 1; n++)
                        {
                            var seg2P1 = new DistBetweenLines.Point3D() { X = partPointsInLine2[j].X, Y = partPointsInLine2[j].Y, Z = partPointsInLine2[j].Z };
                            var seg2P2 = new DistBetweenLines.Point3D() { X = partPointsInLine2[j + 1].X, Y = partPointsInLine2[j + 1].Y, Z = partPointsInLine2[j + 1].Z };

                            DistBetweenLines.Line line2 = new DistBetweenLines.Line() { Start = seg2P1, End = seg2P2 };


                            var targetLineTmp = DistBetweenLines.CalculateDistBetweenLines(line1, line2);
                            if (targetLineTmp == null) 
                                continue;
                            if (targetLineTmp.Length > minDis)
                            {
                                continue;
                            }
                            if (minDisLine == null)
                            {
                                minDisLine = targetLineTmp;
                            }
                            if (targetLineTmp.Length < minDisLine.Length)
                            {
                                minDisLine = targetLineTmp;
                            }
                        }
                    }
                }
            }

            if(minDisLine != null)
            {
                var p1 = new Point3D() { X = minDisLine.Start.X, Y = minDisLine.Start.Y, Z = minDisLine.Start.Z };
                var p2 = new Point3D() { X = minDisLine.End.X, Y = minDisLine.End.Y, Z = minDisLine.End.Z };
                Point3Ds ps = new Point3Ds(new Point3D[] { p1, p2 });
                GeoLine3D gl3 = new GeoLine3D(ps);
                Scene.TrackingLayer.Add(gl3, string.Empty);                
            }*/
            #endregion
        }
        public override void AfterClose()
        {
            this.SceneControl.ObjectSelected -= SceneControl_ObjectSelected;
            this.SceneControl.Scene.TrackingLayer.Clear();
            this.SceneControl.Action = Action3D.Pan;
            base.AfterClose();
        }
        public override void ShowResult(object data, DockPanel dp = null, string resultForm = "执行结果")
        {
            return;
        }
        public override object Do(DockPanel dockPanel = null)
        {
            return null;
        }
        private void InitializeComponent()
        {
            this.uiSymbolLabel1 = new Sunny.UI.UISymbolLabel();
            this.uiSymbolLabel2 = new Sunny.UI.UISymbolLabel();
            this.uiLabel1 = new Sunny.UI.UILabel();
            this.uiLabel2 = new Sunny.UI.UILabel();
            this.uiButtonSelect1 = new Sunny.UI.UIButton();
            this.uiButtonSelect2 = new Sunny.UI.UIButton();
            this.uiLine1 = new Sunny.UI.UILine();
            this.uiLine2 = new Sunny.UI.UILine();
            this.SuspendLayout();
            // 
            // uiSymbolLabel1
            // 
            this.uiSymbolLabel1.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiSymbolLabel1.Location = new System.Drawing.Point(11, 16);
            this.uiSymbolLabel1.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiSymbolLabel1.Name = "uiSymbolLabel1";
            this.uiSymbolLabel1.Padding = new System.Windows.Forms.Padding(28, 0, 0, 0);
            this.uiSymbolLabel1.Size = new System.Drawing.Size(106, 35);
            this.uiSymbolLabel1.Symbol = 61918;
            this.uiSymbolLabel1.SymbolColor = System.Drawing.Color.DodgerBlue;
            this.uiSymbolLabel1.TabIndex = 0;
            this.uiSymbolLabel1.Text = "管线一：";
            // 
            // uiSymbolLabel2
            // 
            this.uiSymbolLabel2.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiSymbolLabel2.Location = new System.Drawing.Point(11, 68);
            this.uiSymbolLabel2.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiSymbolLabel2.Name = "uiSymbolLabel2";
            this.uiSymbolLabel2.Padding = new System.Windows.Forms.Padding(28, 0, 0, 0);
            this.uiSymbolLabel2.Size = new System.Drawing.Size(106, 35);
            this.uiSymbolLabel2.Symbol = 61918;
            this.uiSymbolLabel2.SymbolColor = System.Drawing.Color.DodgerBlue;
            this.uiSymbolLabel2.TabIndex = 0;
            this.uiSymbolLabel2.Text = "管线二：";
            // 
            // uiLabel1
            // 
            this.uiLabel1.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiLabel1.Location = new System.Drawing.Point(104, 22);
            this.uiLabel1.Name = "uiLabel1";
            this.uiLabel1.Size = new System.Drawing.Size(247, 23);
            this.uiLabel1.TabIndex = 1;
            this.uiLabel1.Text = "选择的管线";
            this.uiLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // uiLabel2
            // 
            this.uiLabel2.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiLabel2.Location = new System.Drawing.Point(104, 74);
            this.uiLabel2.Name = "uiLabel2";
            this.uiLabel2.Size = new System.Drawing.Size(247, 23);
            this.uiLabel2.TabIndex = 1;
            this.uiLabel2.Text = "选择的管线";
            this.uiLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // uiButtonSelect1
            // 
            this.uiButtonSelect1.Cursor = System.Windows.Forms.Cursors.Hand;
            this.uiButtonSelect1.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiButtonSelect1.Location = new System.Drawing.Point(355, 16);
            this.uiButtonSelect1.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiButtonSelect1.Name = "uiButtonSelect1";
            this.uiButtonSelect1.Size = new System.Drawing.Size(100, 35);
            this.uiButtonSelect1.TabIndex = 2;
            this.uiButtonSelect1.Text = "选 择";
            // 
            // uiButtonSelect2
            // 
            this.uiButtonSelect2.Cursor = System.Windows.Forms.Cursors.Hand;
            this.uiButtonSelect2.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiButtonSelect2.Location = new System.Drawing.Point(355, 68);
            this.uiButtonSelect2.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiButtonSelect2.Name = "uiButtonSelect2";
            this.uiButtonSelect2.Size = new System.Drawing.Size(100, 35);
            this.uiButtonSelect2.TabIndex = 2;
            this.uiButtonSelect2.Text = "选 择";
            // 
            // uiLine1
            // 
            this.uiLine1.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiLine1.Location = new System.Drawing.Point(60, 54);
            this.uiLine1.MinimumSize = new System.Drawing.Size(2, 2);
            this.uiLine1.Name = "uiLine1";
            this.uiLine1.Size = new System.Drawing.Size(360, 2);
            this.uiLine1.TabIndex = 3;
            // 
            // uiLine2
            // 
            this.uiLine2.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiLine2.Location = new System.Drawing.Point(60, 107);
            this.uiLine2.MinimumSize = new System.Drawing.Size(2, 2);
            this.uiLine2.Name = "uiLine2";
            this.uiLine2.Size = new System.Drawing.Size(360, 2);
            this.uiLine2.TabIndex = 3;
            // 
            // JJAnalyst
            // 
            this.Controls.Add(this.uiLine2);
            this.Controls.Add(this.uiLine1);
            this.Controls.Add(this.uiButtonSelect2);
            this.Controls.Add(this.uiButtonSelect1);
            this.Controls.Add(this.uiLabel2);
            this.Controls.Add(this.uiLabel1);
            this.Controls.Add(this.uiSymbolLabel2);
            this.Controls.Add(this.uiSymbolLabel1);
            this.Name = "JJAnalyst";
            this.Size = new System.Drawing.Size(460, 126);
            this.ResumeLayout(false);

        }
    }
}
